package com.glela.goods.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.glela.goods.dao.LimitTimeSaleDetailDao;
import com.glela.goods.model.LimitTimeSaleDetail;
import com.glela.goods.service.LimitTimeSaleDetailService;

@Service
public class LimitTimeSaleDetailServiceImpl implements LimitTimeSaleDetailService {
	@Autowired
	private LimitTimeSaleDetailDao limitTimeSaleDetailDao;

	@Override
	public LimitTimeSaleDetail queryByProductId(long productId, Date time) {
		return limitTimeSaleDetailDao.selectByProductId(productId, time);
	}

	@Override
	public Map<Long, LimitTimeSaleDetail> queryByProductIds(Set<Long> productIds, Date nowTime) {
		List<LimitTimeSaleDetail> limitTimeSaleDetails = limitTimeSaleDetailDao.selectByProductIds(productIds, nowTime);
		Map<Long, LimitTimeSaleDetail> rsltMap = new HashMap<>();
		if (CollectionUtils.isNotEmpty(limitTimeSaleDetails)) {
			for (LimitTimeSaleDetail limitTimeSaleDetail : limitTimeSaleDetails) {
				rsltMap.put(limitTimeSaleDetail.getSkuNo(), limitTimeSaleDetail);
			}
		}
		return rsltMap;
	}

	@Override
	public List<LimitTimeSaleDetail> queryByGoodsId(long goodsId, Date time) {
		return limitTimeSaleDetailDao.selectByGoodsId(goodsId, time);
	}

	@Override
	public Map<Long, Map<Long, LimitTimeSaleDetail>> queryByGoodsIds(Collection<Long> goodsIds, Date time) {
		Map<Long, Map<Long, LimitTimeSaleDetail>> rstMap = new HashMap<>();
		List<LimitTimeSaleDetail> a = limitTimeSaleDetailDao.selectByGoodsIds(goodsIds, time);
		if (CollectionUtils.isNotEmpty(a)) {
			for (LimitTimeSaleDetail limitTimeSaleDetail : a) {
				if (limitTimeSaleDetail == null) {
					continue;
				}
				if (rstMap.get(limitTimeSaleDetail.getProductSn()) == null) {
					rstMap.put(limitTimeSaleDetail.getProductSn(), new HashMap<Long, LimitTimeSaleDetail>());
				}
				rstMap.get(limitTimeSaleDetail.getProductSn()).put(limitTimeSaleDetail.getSkuNo(), limitTimeSaleDetail);
			}
		}
		return rstMap;
	}
}